package lol.clann.minecraft.springboot.plugin.module.devtools;

import lol.clann.minecraft.springboot.api.bukkit.rawmessage.RawMessage;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;

import java.util.stream.Stream;

/**
 *
 * @author pyz
 * @date 2019/6/12 8:04 PM
 */
public class ListItemUtils {
    public static void listInSingleLine(CommandSender sender, String format, Stream<String> items) {
        RawMessage msg = RawMessage.createRawMessage();
        items.forEach(item -> {
            msg
                    .then(item).color(ChatColor.GREEN).suggestCommand(String.format(format, item))
                    .then(",").color(ChatColor.RED);
        });
        msg.send(sender);
    }

    public static void listLines(CommandSender sender, String format, int perLine, Stream<String> items) {
        int[] counter = new int[1];
        counter[0] = 0;
        RawMessage msg = RawMessage.createRawMessage();
        items.forEach(item -> {
            counter[0]++;
            msg.then(item).color(ChatColor.GREEN).suggestCommand(String.format(format, item)).then(",").color(ChatColor.RED);
            if (counter[0] % perLine == 0) {
                counter[0] = 0;
                msg.send(sender);
                msg.clear();
            }
        });
        if (counter[0] > 0) {
            msg.send(sender);
        }
    }
}
